<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="类型编号" prop="businessCode">
        <el-input v-model="queryParams.businessCode" placeholder="请输入类型编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="类型名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入类型名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
       >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div style="width: 100%;height: 90vh;overflow:auto; margin-bottom: 30%;padding-bottom: 18%;position: relative;">
    <el-table v-loading="loading" :data="uploadList" @selection-change="handleSelectionChange"
      :row-style="{ height: '35px' }" border :cell-style="{ padding: '0px' }" height="400px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类型名称" align="center" prop="name" />
      <el-table-column label="类型编号" align="center" prop="businessCode">
        <template slot-scope="scope">
          <span>{{ scope.row.businessCode }}</span>
          <!-- <router-link :to="'/system/upload-file/index/' + scope.row.businessCode" class="link-type">
            <span>{{ scope.row.businessCode }}</span>
          </router-link> -->
        </template>
      </el-table-column>
      <el-table-column label="文件类型" align="center" prop="type" show-overflow-tooltip="true" />
      <el-table-column label="文件大小" align="center" prop="fileMaxSize">
        <template slot-scope="scope">
          <span>{{ scope.row.fileMaxSize }}MB</span>
        </template>
      </el-table-column>
      <el-table-column label="上传文件个数" align="center" prop="fileLimit" />
      <el-table-column label="备注" align="center" prop="content" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="80">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"  title="编辑"
           ></el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" title="删除"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    </div>
    <!-- 添加或修改附件类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="170px">
        <el-form-item label="类型名称：" prop="name">
          <el-input v-model="form.name" placeholder="请输入附件类型名称" />
        </el-form-item>
        <el-form-item label="类型编号：" prop="businessCode">
          <el-input :disabled="disabled" v-model="form.businessCode" placeholder="请输入类型编号"
            onkeyup="this.value=this.value.replace(/[^\w_]/g,'');" />
        </el-form-item>
        <el-form-item prop="type">
          <span slot="label">
            <el-tooltip content="文件后缀名，如：`.png,.xls`，默认前端配置" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
            文件类型：
          </span>
          <el-input v-model="form.type" placeholder="请输入附件类型名称" />
        </el-form-item>
        <el-form-item label="单次上传文件个数：" prop="fileLimit">
          <el-input-number v-model="form.fileLimit" controls-position="right" :min="1" :max="8" />

        </el-form-item>
        <el-form-item prop="fileMaxSize">
          <span slot="label">
            <el-tooltip :content="`单个文件大小不能超过${file.singleSize},总上传大小不超过${file.allSize}`" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
            单个文件上传大小：
          </span>
          <el-input v-model="form.fileMaxSize" oninput="value=value.replace(/[^0-9.]/g,'')"
            placeholder="请输入单个文件上传大小"><template slot="append">MB</template></el-input>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input type="textarea" v-model="form.content" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUpload, getUpload, delUpload, addUpload, updateUpload, fileSize } from "@/api/system/upload";

export default {
  name: "Upload",
  dicts: ['file_type'],
  data() {
    const validatePass = (rule, value, callback) => {
      let result = this.file.singleSize.replace(/[^0-9]/g, "");
      if (parseFloat(value) > parseFloat(result)) {
        callback(new Error("单个文件上传大小不能超过" + result + "MB"));
      } else if (parseFloat(value) <= 0) {
        callback(new Error("单个文件上传大小不能小于0MB"));
      } else if (value == '' || value == null || value == undefined) {
        callback(new Error("单个文件上传大小为空"));
      }
      else {
        callback();
      }

    };

    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 附件类型表格数据
      uploadList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      disabled: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        businessCode: null,
        name: null,
        type: null,
        content: null,
        operateId: null,
      },
      disabled: false,
      // 表单参数
      form: {},
      file: {},
      // 表单校验
      rules: {
        businessCode: [
          { required: true, message: "类型编号不能为空", trigger: "blur" },
          { max: 20, message: '长度不能超过20', trigger: 'blur' }
        ],
        name: [
          { required: true, message: "类型名称不能为空", trigger: "blur" },
          { max: 50, message: '长度不能超过50', trigger: 'blur' }
        ],
        type: [
          { required: true, message: "文件类型不能为空", trigger: "blur" },
          { max: 50, message: '文件类型不能超过50', trigger: 'blur' }
        ],
        fileLimit: [
          { required: true, message: "单次上传文件个数不能为空", trigger: "blur" },
        ],
        fileMaxSize: [
          { required: true, validator: validatePass, trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询附件类型列表 */
    getList() {
      this.loading = true;
      listUpload(this.queryParams).then(response => {
        this.uploadList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getFileSize() {
      fileSize().then(response => {
        this.file = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        Id: null,
        businessCode: null,
        name: null,
        type: null,
        content: null,
        operateId: null,
        updateBy: null,
        updateTime: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.Id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.disabled = false;
      this.open = true;
      this.getFileSize();
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.disabled = true
      this.getFileSize();
      const Id = row.id || this.ids
      getUpload(Id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUpload(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUpload(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function () {
        return delUpload(Ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/upload/export', {
        ...this.queryParams
      }, `upload_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
